import os
import re
import shutil

file_path = r"D:\temp\以太坊"
p=r" \(.*\..*"
if __name__ == '__main__':
    files = os.listdir(file_path)
    for file in files:
        raw_file_path = os.path.join(file_path,file)
        if os.path.isfile(raw_file_path):
            if file.endswith("xml") :
                pass
                os.remove(raw_file_path)
                # print("remove"+raw_file_path)
            else:
                # Python-微信小程序开发实战（2020） (P184. day16 15 django 离线脚本补充).mp4
                tempfile = re.findall(p,file)
                if len(tempfile) == 1:
                    tempfile = tempfile[0]
                    tempdir = file.replace(tempfile,"")
                    tempdir = tempdir.strip()
                    # print(tempdir,tempfile)
                    new_file_path = os.path.join(file_path,tempdir+os.sep+tempfile.strip())

                    root_dir = os.path.dirname(new_file_path)
                    if not os.path.exists(root_dir):
                        os.makedirs(root_dir)
                    # print(raw_file_path + "--move-->" + new_file_path)

                    shutil.move(raw_file_path,new_file_path)
                else:
                    print(file + "字符串规则无法被识别 "+p)